package cn.hfatec.shms.system.roleAuth.service;

import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.system.roleAuth.dao.RoleAuthDao;
import cn.hfatec.shms.system.roleAuth.model.RoleAuth;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("RoleAuthService")
public class RoleAuthService {

    public static final String BEAN_ID = "RoleAuthService";

    //注入Dao层
    @Resource(name = "RoleAuthDao")
    private RoleAuthDao roleAuthDao;

    /**
     *
     * 根据角色ID，查询已分配的权限
     *
     * @param roleId
     * @return
     */
    @Transactional
    @MysqlDataSource
    public List queryAuthListByRoleId(String roleId) {
        return roleAuthDao.queryAuthListByRoleId(roleId);
    }

    /**
     *
     * 为角色分配权限
     *
     * @param roleId
     * @param ids
     */
    @Transactional
    @MysqlDataSource
    public void assignAuth(String roleId, String[] ids) {

        roleAuthDao.deleteRoleAuthBatchByRoleId(roleId);
        if(ids == null) {
            ids = new String[]{};
        }
        for(String authId : ids) {
            RoleAuth roleAuth = new RoleAuth();
            roleAuth.setAuthId(authId);
            roleAuth.setRoleId(roleId);
            roleAuthDao.insert(roleAuth);
        }
    }
}